import { http } from "@/utils/http";
import type { SREntity } from "@/api/entity";
import type { PDataRes, PListRes, SRPage } from "@/api/response";

export type ModuleTable = SREntity & {
  domain: string;
  className: string;
  dbTable?: string;
  comment?: string;
  domainRoute?: string;
  classRoute?: string;
  auth?: string;
  fields?: ModuleField[];
};

export type QuickSearchType = {
  match: string;
};
export type ValidationType = {
  required: boolean;
};

export type ModuleField = SREntity & {
  domain: string;
  className: string;
  fieldName: string;
  isPk: number;
  isIndex: number;
  isUnique: number;
  jsonName: string;
  goType: string;
  tsType: string;
  quickSearch: QuickSearchType;
  validation: ValidationType;
  comment: string;
  inSearch: number;
  inCreate: number;
  inEdit: number;
};

export const getModuleTableList = (
  req: SRPage & ModuleTable
): Promise<PListRes<ModuleTable>> => {
  return http.get("/api/v1/devtools/moduleTable/list", { params: req });
};

export const getModuleTableDetails = (
  ent: ModuleTable
): Promise<PDataRes<ModuleTable>> => {
  return http.get(`/api/v1/devtools/moduleTable/info`, { params: ent });
};

export const createModuleTable = (data: ModuleTable): Promise<PDataRes> => {
  return http.post("/api/v1/devtools/moduleTable", { data });
};

export const updateModuleTable = (data: ModuleTable): Promise<PDataRes> => {
  return http.put("/api/v1/devtools/moduleTable", { data });
};

export const deleteModuleTable = (ent: ModuleTable): Promise<PDataRes> => {
  return http.delete("/api/v1/devtools/moduleTable", { params: ent });
};
